package com.sjy.shopmall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sjy.shopmall.dao.UserMapper;
import com.sjy.shopmall.pojo.User;
import com.sjy.shopmall.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public int register(String username, String password) {
        try {
            User user = new User();
            user.setPic("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            user.setUserName(username);
            user.setPassword(password);
            userMapper.insert(user);
        } catch (Exception e) {
            throw new RuntimeException("注册失败");
        }

        return 1;
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public String login(String username, String password) {
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        List<User> result = userMapper.select(user);
        if (CollUtil.isNotEmpty(result)) {
            return result.get(0).getId().toString();
        }
        return null;
    }
}
